package StudyCode.summary.zipfile;

import sun.misc.BASE64Decoder;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class DecompressesInputStreamUsingGZIP {
	public static byte[] gunzip(InputStream inputStream) throws IOException {
		byte[] buffer = new byte[1024];
		int count;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		GZIPInputStream gzipIn = new GZIPInputStream(inputStream);

		while ((count = gzipIn.read(buffer)) != -1) {
			baos.write(buffer, 0, count);
		}

		gzipIn.close();

		return baos.toByteArray();
	}


	public static byte[] gunzip(byte[] binaryInput) throws IOException {
		ByteArrayInputStream bais = new ByteArrayInputStream(binaryInput);
		return gunzip(bais);
	}


	public static byte[] gunzip(String base64Input) throws IOException {
		BASE64Decoder base64Decoder = new BASE64Decoder();
		byte[] binaryInput = base64Decoder.decodeBuffer(base64Input);
		return gunzip(binaryInput);
	}
}
